package com.ithaodong.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.ithaodong.common.exception.LeaseException;
import com.ithaodong.model.entity.*;
import com.ithaodong.common.result.ResultCodeEnum;
import com.ithaodong.model.enums.ItemType;
import com.ithaodong.web.admin.mapper.*;
import com.ithaodong.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ithaodong.web.admin.vo.apartment.ApartmentDetailVo;
import com.ithaodong.web.admin.vo.apartment.ApartmentItemVo;
import com.ithaodong.web.admin.vo.apartment.ApartmentQueryVo;
import com.ithaodong.web.admin.vo.apartment.ApartmentSubmitVo;
import com.ithaodong.web.admin.vo.fee.FeeValueVo;
import com.ithaodong.web.admin.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        super.saveOrUpdate(apartmentSubmitVo);
        if (isUpdate) {
            //删除图片列表
            Long apartmentId = apartmentSubmitVo.getId();
            LambdaQueryWrapper<GraphInfo> graphWrapper = new LambdaQueryWrapper<>();
            graphWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphWrapper.eq(GraphInfo::getItemId, apartmentId);
            graphInfoService.remove(graphWrapper);

            //删除配套列表
            LambdaQueryWrapper<ApartmentFacility> apartmentWrapper = new LambdaQueryWrapper<>();
            apartmentWrapper.eq(ApartmentFacility::getApartmentId, apartmentId);
            apartmentFacilityService.remove(apartmentWrapper);

            //删除标签列表
            LambdaQueryWrapper<ApartmentLabel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApartmentLabel::getApartmentId, apartmentId);
            apartmentLabelService.remove(queryWrapper);

            //删除杂费列表
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentId);
            apartmentFeeValueService.remove(apartmentFeeValueWrapper);
        }
        //插入图片列表
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            List<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
        //插入配套列表
        List<Long> submitVoFacility = apartmentSubmitVo.getFacilityInfoIds();
        List<ApartmentFacility> facilityList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(submitVoFacility)) {
            for (Long aLong : submitVoFacility) {
                ApartmentFacility facility = new ApartmentFacility();
                facility.setApartmentId(apartmentSubmitVo.getId());
                facility.setApartmentId(aLong);
                facilityList.add(facility);
            }
        }
        apartmentFacilityService.saveBatch(facilityList);
        //插入标签列表
        List<Long> voLabelIds = apartmentSubmitVo.getLabelIds();
        List<ApartmentLabel> labelList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(voLabelIds)) {
            for (Long voLabelId : voLabelIds) {
                ApartmentLabel apartmentLabel = new ApartmentLabel();
                apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                apartmentLabel.setApartmentId(voLabelId);
                labelList.add(apartmentLabel);
            }
        }
        apartmentLabelService.saveBatch(labelList);
        //插入杂费列表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        List<ApartmentFeeValue> feeValueList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue feeValue = new ApartmentFeeValue();
                feeValue.setApartmentId(apartmentSubmitVo.getId());
                feeValue.setApartmentId(feeValueId);
                feeValueList.add(feeValue);
            }
        }
        apartmentFeeValueService.saveBatch(feeValueList);
    }

    @Override
    public IPage<ApartmentItemVo> pageItem(IPage<ApartmentItemVo> page1, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageApartmentItemByQuery(page1,queryVo);
    }
}




